﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ADB_QuanLyThuVien
{
    public partial class frmAddUser : DevExpress.XtraEditors.XtraForm
    {
        public frmAddUser()
        {
            InitializeComponent();
        }

        private void frmAddUser_Load(object sender, EventArgs e)
        {
            List<Member> lEmployee = new List<Member>();
            lEmployee = DAO_Member.SelectAllEmployee();
            dataGridView1.DataSource = lEmployee;
            List<Account> lAccount = new List<Account>();
            lAccount = DAO_Member.SellectAccEmployee();
            dataGridView2.DataSource = lAccount;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (DAO_Member.isEmail(emailTextBox.Text) == true)
                MessageBox.Show("Email đã có người sử dụng...");
            else
            {
                Member nv = new Member();
                nv.Name = nameTextBox.Text;
                nv.Birthday = Convert.ToDateTime(birthdayDateTimePicker.Text);
                nv.Email = emailTextBox.Text;
                nv.Address = addressTextBox.Text;
                nv.PhoneNumber = phoneNumberTextBox.Text;
                nv.Sex = (chkGioiTinh.Checked) ? "Nam" : "Nữ";
                if (DAO_Account.isUsername(txtUsername.Text) == true)
                    MessageBox.Show("Tên đăng nhập đã tồn tại...");
                else
                {

                    if (txtRePassword.Text != txtPassword.Text)
                    {
                        MessageBox.Show("Mật khẩu bạn nhập chưa chính xác...");
                        txtPassword.Text = "";
                        txtRePassword.Text = "";
                    }
                    else
                    {
                        Account acc = new Account();
                        acc.Username = txtUsername.Text;
                        acc.Password = txtPassword.Text;
                        acc.DateCreate = DateTime.Today;
                        nv.Acc = acc;
                        bool kq = DAO_Member.AddEmployee(nv);
                        if (kq == true)
                            MessageBox.Show("Thêm Nhân viên thành công");
                        frmAddUser_Load(sender, e);
                    }
                }
            }
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            frmAddUser_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DAO_Member.RemoveMember(emailTextBox.Text);
        }
    }
}